import {getAreaCountryList, getAreaList, getCountryList} from '@/api/appsystem/area'

const useAreaStore = defineStore(
    'area',
    {
        state: () => ({
            city: [],
            area: [],
            areaInCity: {}
        }),
        actions: {
            // 登录
            getCities() {
                return new Promise((resolve, reject) => {
                    if (this.city.length > 0) {
                        resolve(this.city);
                    } else {
                        getCountryList().then(res => {
                            this.city = res.data;
                            resolve(this.city);
                        }).catch(error => {
                            reject(error);
                        });
                    }
                });
            },
            // 获取用户信息
            getAreas() {
                return new Promise((resolve, reject) => {
                    if (this.area.length > 0) {
                        resolve(this.area);
                    } else {
                        getAreaList().then(res => {
                            this.area = res.data;
                            resolve(this.area)
                        }).catch(error => {
                            reject(error)
                        });
                    }
                })
            },
            // 退出系统
            getAreasInCity(cityCode) {
                return new Promise((resolve, reject) => {
                    let aInCi = this.areaInCity[cityCode];
                    if (aInCi && aInCi.length > 0) {
                        resolve(aInCi);
                    } else {
                        getAreaCountryList(cityCode).then(res => {
                            this.areaInCity[cityCode] = res.data
                            resolve(this.areaInCity[cityCode])
                        }).catch(error => {
                            reject(error)
                        });
                    }
                });
            }
        }
    })

export default useAreaStore
